package com.boyi.center.controller.impl;


import com.boyi.center.controller.BaseController;
import com.boyi.center.service.BaseService;
import com.boyi.shared.common.Page;
import com.boyi.shared.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author tingwen
 * @date 2017/3/28 21:06
 * @description
 */
@RestController
public abstract class BaseControllerImpl<T> implements BaseController<T> {
    @Autowired
    protected BaseService<T> baseService;

   /* @PostConstruct
    public void init() {
        Type type = this.getClass().getGenericSuperclass();
        //得到当前类上所有的泛型类型Class
        Type[] types = ((ParameterizedType) type).getActualTypeArguments();
        //得到具体传入的泛型类Class对象
        baseService = CM.getBeanByType(BaseService.class, types[0]);
    }*/

    @GetMapping("save")
    @Override
    public Result<List<T>> save(@RequestBody T entity) {
        return baseService.save(entity);
    }

    @GetMapping("batchInsert")
    @Override
    public Result<List<T>> batchInsert(@RequestBody List<T> entities) {
        return baseService.batchInsert(entities);
    }

    @GetMapping("delete")
    @Override
    public Result<List<T>> delete(@RequestBody T entity) {
        return baseService.deleteByPrimaryKey(entity);
    }

    @GetMapping("updateNotNull")
    @Override
    public Result<List<T>> updateNotNull(@RequestBody T entity) {
        return baseService.updateByPrimaryKeySelective(entity);
    }

    @GetMapping("selectPage")
    @Override
    public Result<List<T>> selectPage(T entity, Page page) {
        if (page.getPageNum() == 0 || page.getPageSize() == 0) {
            return baseService.select(entity);
        }
        return baseService.selectPageByExample(entity, page.getPageNum(), page.getPageSize());
    }

    @GetMapping("batchDelete")
    @Override
    public Result<List<T>> batchDelete(@RequestBody String[] ids) {
        return baseService.batchDeleteByPrimaryKeys(ids);
    }

}
